#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
assert(sys.version_info) >= (2, 6, 0), 'python >= 2.6.0 required'
import gtk
import ConfigParser

from constants import *

class ArkayaEmulatorEditWindow:
	def __init__(self, builder):


		#builder = gtk.Builder()
		#builder.add_from_file(self.gladefile)
		
		self.window = builder.get_object("settings_window")

class ArkayaSetup:
	def on_window_destroy(self, widget, data = None):
		gtk.main_quit()
		
	def on_quit_menu_item_activate(self, *args):
		gtk.main_quit()

	def on_about_menu_item_activate(self, *args):
		about_dialog = gtk.AboutDialog()

		about_dialog.set_name("Arkaya")
		about_dialog.set_logo(gtk.gdk.pixbuf_new_from_file(os.path.join(APP_PATH, 'glade', 'logo.png')))
		about_dialog.set_authors(['Jost Stricker Jørgensen', 'Troels Rasmussen'])
		about_dialog.set_website('http://code.google.com/p/arkaya/')
		about_dialog.set_website_label('Google code http://code.google.com/p/arkaya/')
		about_dialog.set_copyright('%s 2010-2011 Jost Stricker Jørgensen' % (unichr(169)).encode("utf-8"))
		about_dialog.set_license(open(os.path.join(APP_PATH, 'docs', 'gpl-3.0.txt')).read())
		about_dialog.set_version(VERSION)

		about_dialog.run()
		about_dialog.destroy()

	def on_remove_button_clicked(self, *args):
		(model, itr) = self.installed_list.get_selection().get_selected()
		try:
			status = "Emulator " + model.get_value(itr, 0) + " removed from installation"
		except TypeError:
			status = "No emulator selected"

		self.statusbar.push(0, status)
		return True

	def on_edit_button_clicked(self, *args):
		edit_window = ArkayaEmulatorEditWindow(self.builder)
		edit_window.window.show()

		(model, itr) = self.available_list.get_selection().get_selected()
		entry = model.get_value(itr, 0)
		
		print entry
		return True

	def on_add_button_clicked(self, *args):		
		(model, itr) = self.available_list.get_selection().get_selected()
		try:
			entry = "Emulator " + model.get_value(itr, 0) + " added to instalation"
		except TypeError:
			entry = "No emulator selected"
			
		self.statusbar.push(0, entry)
		return True

	def load_installed_emulators(self):
		ini_file = os.path.join(APP_PATH, 'arkaya.ini')

		parser = ConfigParser.ConfigParser()		
		parser.read(ini_file)
		
		setup_dir = parser.get('general', 'configuration_dir')
		config_dir = os.path.expanduser(setup_dir)
		if not os.path.exists(config_dir):
			os.mkdir(config_dir)
			self.statusbar.push(0, "Config dir " + config_dir + " created")

		setup_files = os.listdir(config_dir)
		
		list_store = gtk.ListStore(str)
		
		for setup_file in setup_files:
			sf = os.path.join(APP_PATH, 'config', setup_file)

			parser.read(sf)

			name = parser.get('emulator', 'emulator_name')

			list_store.append([name])

		return list_store

	def load_configuration_file(self):
		template_dir = APP_PATH + '/templates'

		config_files = os.listdir(template_dir)

		list_store = gtk.ListStore(str)

		parser = ConfigParser.ConfigParser()

		for config_file in config_files:
			cf = os.path.join(template_dir, config_file)

			parser.read(cf)

			name = parser.get('emulator', 'emulator_name')

			list_store.append([name])

		return list_store

	def __init__(self, glade_file):
		self.gladefile = glade_file
		self.builder = gtk.Builder()
		self.builder.add_from_file(self.gladefile)

		self.statusbar = self.builder.get_object("statusbar")

		list_store = self.load_configuration_file()
		a_list_store = self.load_installed_emulators()

		self.window = self.builder.get_object("window")
		
		self.available_list = self.builder.get_object("available_tree_view")
		self.installed_list = self.builder.get_object("installed_tree_view")
		#print list_store
		self.available_list.set_model(list_store)
		self.installed_list.set_model(a_list_store)
		

		
		print self.available_list
		print self.available_list.get_model()
		
		self.builder.connect_signals(self)


if __name__ == "__main__":

	# Instantiate setup gui class
	app = ArkayaSetup(GLADE_SETUP_FILE)
	app.window.show()
	gtk.main()
